package com.itheima.dao;

import com.itheima.controller.BaseStudentController;
import com.itheima.domain.Student;

import java.util.ArrayList;

/*
学生  数据操作类
数组

增
删
改
查
 */
public class OtherStudentDao implements BaseStudentDao {
    //创建数组
    private static ArrayList<Student> students = new ArrayList<>();

    static {
        //数据的初始化工作
        Student student1 = new Student("0001", "坤坤1", 26, "1996");
        Student student2 = new Student("0002", "坤坤2", 26, "1996");
        Student student3 = new Student("0003", "坤坤3", 26, "1996");
        Student student4 = new Student("0004", "坤坤4", 26, "1996");

        students.add(student1);
        students.add(student2);
        students.add(student3);
        students.add(student4);
    }
    // 真正的向数组中添加数据
    @Override
    public boolean addStudent(Student student) {
        students.add(student);
        return true;
    }

    public Student[] findAllStudents() {
        //students  -> 数组
       /* Student[] stus = new Student[OtherStudentDao.students.size()];
        for (int i = 0; i < students.size(); i++) {
            stus[i]= students.get(i);
        }
*/
        Student[] stus = new Student[OtherStudentDao.students.size()];
        students.toArray(stus);
        return stus;

    }

    public void deleteStudent(String sid) {
        //找人
        int index = getIndex(sid);
        students.remove(index);
    }

    public void updateStudent(String sid, Student student) {
        //找人
        int index = getIndex(sid);
       students.set(index,student);

    }

    //根据条件查
    public  int getIndex(String sid){
        int index=-1;
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            if(student!=null&& student.getSid().equals(sid)){
                index=i;
                break;
            }
        }
        return index;

    }
}
